package com.example.javaproject.thread.juc.threadpool;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.Vector;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * Description:  线程池测试
 * <br/>Author:  tianjinyong
 * <br/>Date:    2020/7/9 3:04 PM
 */
public class ThreadPoolExecutorTest {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
//        testExecute();
        testSubmit();
    }

    /**
     * execute方法
     */
    public static void testExecute() {
        ThreadPoolExecutor executor = new ThreadPoolExecutor(
                4, 5, 0, TimeUnit.SECONDS, new LinkedBlockingDeque<>()
        );

        executor.execute(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName());
            }
        });

        executor.shutdown();
    }

    /**
     * 测试 submit方法
     *
     * @throws ExecutionException
     * @throws InterruptedException
     */
    public static void testSubmit() throws ExecutionException, InterruptedException {
        ThreadPoolExecutor executor = new ThreadPoolExecutor(
                4, 5, 0, TimeUnit.SECONDS, new LinkedBlockingDeque<>()
        );

        List<Future<String>> futureList = new Vector<>();
        for (int i = 0; i < 10; i++) {
            futureList.add(executor.submit(new Callable<String>() {
                @Override
                public String call() throws Exception {
                    return Thread.currentThread().getName();
                }
            }));
        }

        for (int i = 0; i < futureList.size(); i++) {
            String rf = futureList.get(i).get();
            System.out.println(rf);
        }

        executor.shutdown();
    }


}

